package encoding

import "strings"

type Codec interface {
	Marshal(v any) ([]byte, error)
	Unmarshal(data []byte, v any) error
	Name() string
}

var registeredCodec = make(map[string]Codec)

func RegisterCode(c Codec) {
	if c == nil {
		panic("cannot register a nil Codec")
	}
	if c.Name() == "" {
		panic("cannot register Codec with empty string result for Name()")
	}
	contentSubtype := strings.ToLower(c.Name())
	registeredCodec[contentSubtype] = c
}

func GetCodec(contentSubtype string) Codec {
	return registeredCodec[contentSubtype]
}
